package com.nondo.dean.dealer.controller.api.app;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.Activity;
import com.nondo.dean.dealer.model.param.ActivityParam;
import com.nondo.dean.dealer.model.result.ActivityDetailResult;
import com.nondo.dean.dealer.model.result.ActivityProductResult;
import com.nondo.dean.dealer.model.result.ActivityResult;
import com.nondo.dean.dealer.service.ActivityService;
import com.nondo.dean.dealer.service.StoreProductService;
import com.nondo.dean.dealer.service.UserService;

/**
 * @author xiaofan
 * @Description: 活动
 * @throws
 * @date 2018-05-0910:18
 * @Version 0.1
 */
@RestController("apiAppDealerActivity")
@RequestMapping("/{v}/app/activity")
public class ActivityController {

    @Autowired
    AliyunOssTool aliyunOssTool;
    
    @Autowired
    UserService userService;
    
    @Autowired
    ActivityService activityService;
    
    @Autowired
    StoreProductService storeProductService;
    
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
    
    /**
     * 有效期1天
     */
    private static final long EXPIRE_TIME = 1 * 24 * 3600;

    /**
     * 活动产品
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/product")
    public View<List<ActivityProductResult>> activityProduct(HttpServletRequest request, HttpServletResponse response) {
        View<List<ActivityProductResult>> view = new View<List<ActivityProductResult>>(20000, "获取数据成功");

        List<ActivityProductResult> list = storeProductService.getActivityProductResultList();

        view.setData(list);

        return view;
    }
    /**
     * 新建活动
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add/activity")
    public View<Integer> addActivity(HttpServletRequest request, HttpServletResponse response,
                                                 @Validated @RequestBody ActivityParam activityParam, BindingResult result) {
        View<Integer> view = new View<Integer>(20000, "添加成功");
        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Integer activityId = this.activityService.addAppActivity(activityParam);
            view.setData(activityId);
        }


        return view;
    }

    /**
     * 活动管理列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<ActivityResult>> list(HttpServletRequest request, HttpServletResponse response,
                                          @RequestBody Query<Activity> query) {
        View<Page<ActivityResult>> view = new View<Page<ActivityResult>>(20000, "获取列表成功");

        Activity activity = query.getData();

        if (activity == null) {
            activity = new Activity();
        }
        
        Integer userId = userService.getLoginUserId();
        activity.setUserId(userId);
        
        Page<ActivityResult> page = new Page<ActivityResult>(query.getPage(), query.getSize(), activityService.count(activity));

        List<ActivityResult> list = activityService.searchActivityList(query.getOffset(), query.getLimit(),activity);
        page.setContent(list);

        view.setData(page);

        return view;
    }


    /**
     * 删除活动
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{id}")
    public View<Integer> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id){
        View view =new View(2000,"删除成功");

        Activity activity=activityService.select(id);
        activity.setDelFlag(Activity.DEL_FLAG_DELETE);
        this.activityService.update(activity);

        return view;
    }

    @RequestMapping("/preview/url")
    @ResponseBody
    public View<String> getPreviewAddress(HttpServletRequest request, HttpServletResponse response,
        @RequestBody ActivityParam activityParam, Integer text) {
        View<String> view = new View<String>(20000, "查看成功");
        
        if (text == 0) {
            ActivityDetailResult beans = activityService.previewActivityDetail(activityParam);
            
            String previewKey = UUID.randomUUID().toString();
            @SuppressWarnings("unchecked")
            ValueOperations<String, ActivityDetailResult> valueOperations = redisTemplate.opsForValue();
            valueOperations.set(previewKey, beans, EXPIRE_TIME, TimeUnit.SECONDS);
            String viewPath = "/wap/app/activity/view?text=0&id=" + previewKey;
            view.setData(viewPath);
        }

        return view;
    }

}
